************** Spatial Regressions ******************
* spatial transformation completed previously in spgen_4_* 

************************************
* Contents
		* Radiant based: section 2 horizontal, 6 vertical, counter-clockwise.
		* 4950 cells based on k_bins of width 3 KwH 
		* Outcomes: sales and count, each in level, log+1, and ihs transformation
		* standard errors clustered by cell   
************************************

************************************
* Variables
		* cm: country-month items (second level identifier)
		* cell: 1-50 are bunchers at window = 2. (primary identifier)
		* a_bin/e_bin: individual attributes, unstandardized 
		* csd  / ccd: sales / product count in cell. 
		
		
		** WRONG! xtreg y x1 x2 ... xK, fe 
		/* example from wooldridge  
xtreg y x1 x2 ... xK, fe
predict u1h, e
gen uh1sq = uh1^2
xtreg y z1 ... zM, fe
predict uh2, e
gen uh2sq = uh2^2
gen diff = uh1sq - uh2sq
reg diff, cluster(csid)
		*/ 
		
capture log close 
clear all
set more off 
*set maxvar 30000, permanently // required to load matrix 
* ssc install reghdfe 
* ssc install ftools

* Set directory
global server "R:\WSV2\TBu_AKe\Spatial_NEW" 
cd "$server" // set working directory 

capture mkdir Wmat_SCB_test
global store "${server}\Wmat_SCB_test"

*Define sub directories
global desktop "C:\Users\hy65byfe\Desktop\smerge_0712"
*cd "$desktop" // set working directory 


cd "$store"
 log using spatial_6_regs_s_1502_vuong, replace   
 

			***************************
			* run regression: OLS, fe *
			***************************	
			
cd "C:\Users\hy65byfe\Desktop\smerge_0712\SCB_4950"	
*cd "R:\WSV2\TBu_AKe\Spatial_NEW\SCB_4950"	
use spatial_regs_s_0611_monthly, replace

*cd "C:\Users\hy65byfe\Desktop\smerge_0712\SCB_4950"			
*use spatial_regs_s_monthly_0102_overlap, replace  
			
*preserve
*spmatrix dir 
keep if year == 2014 // no changes. otherwise matrices are wrong in previous. 	
	
	
			***************************
			***** Single Matrix *****
			***************************	
		

** log and ihs transformations to the untransformed data **

		*** outcomes and polar cases (no q indexing needed, only double loop)
foreach varx of varlist csd L3_sd  		ccd L3_cd     {

   gen     log_`varx'=log(1+`varx')
   gen     ihs_`varx'=`varx'+(`varx'^2+1)^0.5
   quietly replace ihs_`varx'=log(ihs_`varx')
 }
 
 
		*** spatial variables *** 
forvalues q = 1(1)8 { 
	foreach varx of varlist 	S`q'_L3_sd C`q'_L3_sd B`q'_L3_sd S`q'_L3_cd C`q'_L3_cd B`q'_L3_cd	{

   gen     log_`varx'=log(1+`varx')
   gen     ihs_`varx'=`varx'+(`varx'^2+1)^0.5
   quietly replace ihs_`varx'=log(ihs_`varx')
	}
}

			**********************************************
			***** 			Regressions 			 *****
			**********************************************

			
***************** Baseline Vertical Regression ****************************


    ** Results for log transformed data **
	
	local q = 6
	
	*quietly {
			 
	reghdfe  log_csd    log_L3_sd 	log_S`q'_L3_sd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) residuals(log_V_S`q')
	estimates store log_V_S`q'
	*predict log_V_S`q', e
	gen ll_log_SV = (log_V_S`q')^2
	
	reghdfe  log_csd    log_L3_sd 	log_C`q'_L3_sd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) residuals(log_V_C`q')
	estimates store log_V_C`q'
	*predict log_V_C`q', e
	gen ll_log_CV = (log_V_C`q')^2
	
	reghdfe  log_csd    log_L3_sd 	log_B`q'_L3_sd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) residuals(log_V_B`q')
	estimates store log_V_B`q'
	*predict log_V_B`q', e
	gen ll_log_BV = (log_V_B`q')^2
	
    ** Results for ihs transformed data **

	reghdfe  ihs_csd    ihs_L3_sd 	ihs_S`q'_L3_sd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) residuals(ihs_V_S`q')
	estimates store ihs_V_S`q'
	*predict ihs_V_S`q', e
	gen ll_ihs_SV = (ihs_V_S`q')^2
	
	reghdfe  ihs_csd    ihs_L3_sd 	ihs_C`q'_L3_sd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) residuals(ihs_V_C`q')
	estimates store ihs_V_C`q'
	*predict ihs_V_C`q', e
	gen ll_ihs_CV = (ihs_V_C`q')^2
	
	reghdfe  ihs_csd    ihs_L3_sd 	ihs_B`q'_L3_sd					if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) residuals(ihs_V_B`q')
	estimates store ihs_V_B`q'
	*predict ihs_V_B`q', e
	gen ll_ihs_BV = (ihs_V_B`q')^2
	
	*} 

	estimates table *V*, b(%9.4f) stats(N r2 r2_a F ll) star(0.10 .05 .01) // vertical, column (6) in main tables 

	sum log_V*
	sum ihs_V*
	
******************** Nonnested Alternative **********************************

		
		******************
		*** Fan (S) ******
		******************

    ** Results for log transformed data **
			 
forvalues q = 1(1)8 {
	reghdfe  log_csd    log_L3_sd 	log_S`q'_L3_sd								if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) residuals(log_S`q')
	estimates store log_S`q'
	*predict log_S`q', e
	gen ll_log_S`q' = (log_S`q')^2
	gen diff_log_S`q' = ll_log_SV - ll_log_S`q'
}

*sum diff* // check variables, must be zero for no. 6. 

forvalues q = 1(1)8 {
	reg diff_log_S`q', vce(cluster cell)
	estimates store log_diff_S`q' // constant significant? test for differences in model fit. (Wooldridge explain the by-hand process)
} 

estimates table  log_diff_S* , b(%9.3f) se(%9.3f) stats(N r2) 
estimates table  log_diff_S* , b(%9.3f) 		  stats(N r2) star(0.10 .05 .01)


    ** Results for ihs transformed data **

forvalues q = 1(1)8 {
	reghdfe  ihs_csd    ihs_L3_sd 	ihs_S`q'_L3_sd								if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) residuals(ihs_S`q')
	estimates store ihs_S`q'
	*predict ihs_S`q', e
	gen ll_ihs_S`q' = (ihs_S`q')^2
	gen diff_ihs_S`q' = ll_ihs_SV - ll_ihs_S`q'
}

*sum diff* // check variables, must be zero for no. 6. 

forvalues q = 1(1)8 {
	reg diff_ihs_S`q', vce(cluster cell)
	estimates store ihs_diff_S`q' // constant significant? test for differences in model fit. (Wooldridge explain the by-hand process)
} 

estimates table  ihs_diff_S* , b(%9.3f) se(%9.3f) stats(N r2) 
estimates table  ihs_diff_S* , b(%9.3f) 		  stats(N r2) star(0.10 .05 .01)



		************************
		*** 4 corners (C) ******
		************************
		
    ** Results for log transformed data **
			 
forvalues q = 1(1)8 {
	reghdfe  log_csd    log_L3_sd 	log_C`q'_L3_sd								if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) residuals(log_C`q')
	estimates store log_C`q'
	*predict log_C`q', e
	gen ll_log_C`q' = (log_C`q')^2
	gen diff_log_C`q' = ll_log_CV - ll_log_C`q'
}

*sum diff* // check variables, must be zero for no. 6. 

forvalues q = 1(1)8 {
	reg diff_log_C`q', vce(cluster cell)
	estimates store log_diff_C`q' // constant significant? test for differences in model fit. (Wooldridge explain the by-hand process)
} 

estimates table  log_diff_C* , b(%9.3f) se(%9.3f) stats(N r2) 
estimates table  log_diff_C* , b(%9.3f) 		  stats(N r2) star(0.10 .05 .01)


    ** Results for ihs transformed data **

forvalues q = 1(1)8 {
	reghdfe  ihs_csd    ihs_L3_sd 	ihs_C`q'_L3_sd								if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) residuals(ihs_C`q')
	estimates store ihs_C`q'
	*predict ihs_C`q', e
	gen ll_ihs_C`q' = (ihs_C`q')^2
	gen diff_ihs_C`q' = ll_ihs_CV - ll_ihs_C`q'
}

*sum diff* // check variables, must be zero for no. 6. 

forvalues q = 1(1)8 {
	reg diff_ihs_C`q', vce(cluster cell)
	estimates store ihs_diff_C`q' // constant significant? test for differences in model fit. (Wooldridge explain the by-hand process)
} 

estimates table  ihs_diff_C* , b(%9.3f) se(%9.3f) stats(N r2) 
estimates table  ihs_diff_C* , b(%9.3f) 		  stats(N r2) star(0.10 .05 .01)



		************************
		*** Beam (B) ***********
		************************
		
    ** Results for log transformed data **
			 
forvalues q = 1(1)8 {
	reghdfe  log_csd    log_L3_sd 	log_B`q'_L3_sd								if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) residuals(log_B`q')
	estimates store log_B`q'
	*predict log_B`q', e
	gen ll_log_B`q' = (log_B`q')^2
	gen diff_log_B`q' = ll_log_BV - ll_log_B`q'
}

sum diff* // check variables, must be zero for no. 6. 

forvalues q = 1(1)8 {
	reg diff_log_B`q', vce(cluster cell)
	estimates store log_diff_B`q' // constant significant? test for differences in model fit. (Wooldridge explain the by-hand process)
} 

estimates table  log_diff_B* , b(%9.3f) se(%9.3f) stats(N r2) 
estimates table  log_diff_B* , b(%9.3f) 		  stats(N r2) star(0.10 .05 .01)


    ** Results for ihs transformed data **

forvalues q = 1(1)8 {
	reghdfe  ihs_csd    ihs_L3_sd 	ihs_B`q'_L3_sd								if cell <= 50 & year == 2014, absorb(cell ccode month) vce(cluster cell) residuals(ihs_B`q')
	estimates store ihs_B`q'
	*predict ihs_B`q', e
	gen ll_ihs_B`q' = (ihs_B`q')^2
	gen diff_ihs_B`q' = ll_ihs_BV - ll_ihs_B`q'
}

sum diff* // check variables, must be zero for no. 6. 

forvalues q = 1(1)8 {
	reg diff_ihs_B`q', vce(cluster cell)
	estimates store ihs_diff_B`q' // constant significant? test for differences in model fit. (Wooldridge explain the by-hand process)
} 

estimates table  ihs_diff_B* , b(%9.3f) se(%9.3f) stats(N r2) 
estimates table  ihs_diff_B* , b(%9.3f) 		  stats(N r2) star(0.10 .05 .01)


log close 


log using results_summary_s_1502_vuong, replace 

		********************
		** report results **
		********************
		
		** SETUP : cmx = 4950, matrix = fan/beam/4corners, date = monthly, FE = cell, month, ccode 
		
		**************
		** Fan (S) **
		*************
	
		******* reghdfe LOG w/ country and cell fixed-effects ******
					
estimates table  log_diff_S* , b(%9.3f) 		  stats(N r2) star(0.10 .05 .01)

		******* reghdfe IHS w/ country and cell fixed-effects ******

estimates table  ihs_diff_S* , b(%9.3f) 		  stats(N r2) star(0.10 .05 .01)



		*******************
		** 4 Corners (C) **
		*******************

		******* reghdfe LOG w/ country and cell fixed-effects ******
					
estimates table  log_diff_C* , b(%9.3f) 		  stats(N r2) star(0.10 .05 .01)

		******* reghdfe IHS w/ country and cell fixed-effects ******

estimates table  ihs_diff_C* , b(%9.3f) 		  stats(N r2) star(0.10 .05 .01)


		**************
		** Beam (B) **
		**************

		******* reghdfe LOG w/ country and cell fixed-effects ******
					
estimates table  log_diff_B* , b(%9.3f) 		  stats(N r2) star(0.10 .05 .01)

		******* reghdfe IHS w/ country and cell fixed-effects ******

estimates table  ihs_diff_B*, b(%9.3f) 		  stats(N r2) star(0.10 .05 .01)

log close 
			